home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-07-10 | 2.5 KB | 109 lines |
-
- import java.applet.Applet;
- import java.awt.Graphics;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.util.Date;
- import java.lang.Math;
- import java.lang.Thread;
- import java.lang.System;
- import java.net.URL;
-
-
- public class clock extends Applet implements Runnable {
- int AppletHeight, AppletWidth;
- boolean die;
- int a;
-
- public void init() {
- Dimension d = size();
- AppletHeight = d.height;
- AppletWidth = d.width;
- }
-
- public void paint(Graphics g) {
- painter(g,AppletHeight,AppletWidth);
- }
-
-
- public void tick() {
- repaint();
- try {
- Thread.sleep(1000);
- } catch(InterruptedException e) ;
- }
-
- public void start() {
- die = false;
- (new Thread(this)).start();
- }
-
- public void run() {
- while (!die) tick();
- }
-
-
- public void stop() {
- die = true;
- }
-
- public void painter(Graphics g, int AppletHeight, int AppletWidth) {
-
- int a,p;
- int cx,cy;
- int ix, iy, jx, jy;
- int hour, minute, sec;
- double scale,fraction;
- Date d = new Date();
- Math m = null;
- g.setColor(Color.white);
- g.fillOval(2,2,AppletWidth-4,AppletHeight-4);
- g.setColor(Color.blue);
- cx = (int) AppletWidth / 2;
- cy = (int) AppletHeight / 2;
- p = (int) m.round(0.05 * AppletHeight);
- for (a=1; a<p; a++) {
- g.drawOval(a,a,AppletWidth-(2*a),AppletHeight-(2*a));
- }
- hour = d.getHours();
- minute = d.getMinutes();
- sec = d.getSeconds();
- scale = (2 * 3.1415) / 12;
- for (a=0; a<12; a++) {
- ix = (int) m.round( cx+(0.8*cx*m.cos(a*scale)) );
- iy = (int) m.round( cy+(0.8*cy*m.sin(a*scale)) );
- jx = (int) m.round( cx+(cx*m.cos(a*scale)) );
- jy = (int) m.round( cy+(cy*m.sin(a*scale)) );
- g.drawLine(ix,iy,jx,jy);
-
- }
- // Draw Second Hand
- scale = (2 * 3.1415) / 60;
- ix = (int) m.round( cx );
- iy = (int) m.round( cy );
- jx = (int) m.round( cx+(0.8*cx*m.cos(sec*scale-(3.1415/2))) );
- jy = (int) m.round( cy+(0.8*cy*m.sin(sec*scale-(3.1415/2))) );
- g.setColor(Color.red);
- g.drawLine(ix,iy,jx,jy);
- g.setColor(Color.black);
- // Draw Minute Hand
- scale = (2 * 3.1415) / 60;
- ix = (int) m.round( cx );
- iy = (int) m.round( cy );
- jx = (int) m.round( cx+(0.8*cx*m.cos(minute*scale-(3.1415/2))) );
- jy = (int) m.round( cy+(0.8*cy*m.sin(minute*scale-(3.1415/2))) );
- g.drawLine(ix,iy,jx,jy);
- // Draw Hour Hand
- scale = (2 * 3.1415) / 12;
- ix = (int) m.round( cx );
- iy = (int) m.round( cy );
- fraction = minute * 1.0;
- fraction = fraction / 60.0;
- jx = (int) m.round( cx+(0.5*cx*m.cos((hour + fraction)*scale-(3.1415/2))) );
- jy = (int) m.round( cy+(0.5*cy*m.sin((hour + fraction)*scale-(3.1415/2))) );
- g.drawLine(ix,iy,jx,jy);
- }
-
- }
-
-